
const CONFIGER_MANAGER = {
	DOMAIN: "http://121.41.6.183:9999/"
};
const STORATE_MANAGER = {
	user: "CREDIT_LOGINED_USER",
	queryinfo: "QUERY_INFOR",
	queryresult: "QUERY_RESULT",
	queryParamArray: "QUERY_PARAM_ARRAY",
};

$(function() {
	//显示等待中
	$.showWaiting = function(message) {
		if ($("#message_waiting_pop").length > 0) {
			return;
		}
		var html = [];
		html.push("<div id='message_waiting_pop' class='pop-wrap'  style='background: transparent;'>");
		html.push("<div class='waiting'>");
		if (message) {
			html.push("<div class='msgtop1'></div>");
			html.push("<div class='spinner spinner1'>");
		} else {
			html.push("<div class='msgtop2'></div>");
			html.push("<div class='spinner spinner2'>");
		}
		html.push("<div class='bar1'></div>");
		html.push("<div class='bar2'></div>");
		html.push("<div class='bar3'></div>");
		html.push("<div class='bar4'></div>");
		html.push("<div class='bar5'></div>");
		html.push("<div class='bar6'></div>");
		html.push("<div class='bar7'></div>");
		html.push("<div class='bar8'></div>");
		html.push("<div class='bar9'></div>");
		html.push("<div class='bar10'></div>");
		html.push("<div class='bar11'></div>");
		html.push("<div class='bar12'></div>");
		html.push("</div>");
		if (message) {
			html.push("<div class='msgbottom1 waitingmessage'>" + message + "</div>");
		} else {
			html.push("<div class='msgbottom2'></div>");
		}
		html.push("</div>");
		html.push("</div>");
		html = html.join('');
		$(html).appendTo("body");
	}

	$.closeWaiting = function() {
		if ($("#message_waiting_pop").length > 0) {
			$("#message_waiting_pop").remove();
			return;
		}

	}

	$.alert = function(message, alertCB, title, buttonCapture) {
		if ($("#message_alert_pop").length > 0) {
			return;
		}

		// 默认值
		if (!title) {
			title = "";
		}
		if (!message) {
			message = "";
		}
		if (!buttonCapture) {
			buttonCapture = "好";
		}

		var html = "<div id='message_alert_pop' class='pop-wrap'><div class='passwrod-bank-inp'><h2>{0}</h2><pre class='tishiyu'>{1}</pre><div class='pass-btn-block'><p class='queding1' id='btnalert0'>{2}</p></div></div></div>";

		html = html.format(title, message, buttonCapture);
		$(html).appendTo("body");

		$("#btnalert0").on("tap", function() {
			$("#message_alert_pop").remove();
			if (alertCB) alertCB(0);
		});
	};

	$.confirm = function(message, confirmCB, title, buttons) {
		if ($("#message_confirm_pop").length > 0) {
			return;
		}
		// 默认值
		if (!title) {
			title = "";
		}
		if (!message) {
			message = "";
		}
		if (!buttons) {
			buttons = ["确定", "取消"];
		} else if (buttons.length < 2) {
			$(alert(message, confirmCB, title, buttons[0]));
		}

		var html = "<div id='message_confirm_pop' class='pop-wrap' ><div class='passwrod-bank-inp' style='min-width: 17em;'><h2>{0}</h2><pre class='tishiyu'>{1}</pre><div class='pass-btn-block'><p class='queding' id='btnconfrim0'>{2}</p><p class='quxiao' id='btnconfrim1'>{3}</p></div></div></div>";
		html = html.format(title, message, buttons[0], buttons[1]);
		$(html).appendTo("body");

		var pop = $("#message_confirm_pop");
		$("#btnconfrim0").on("click", function() {
			if (confirmCB) confirmCB(0);
			pop.remove();
		});
		$("#btnconfrim1").on("click", function() {
			if (confirmCB) confirmCB(1);
			pop.remove();
		});
	};

	$.prompt = function(message, promptCB, title, tip, buttons) {
		if ($("#message_prompt_pop").length > 0) {
			return;
		}
		// 默认值
		if (!title) {
			title = "";
		}
		if (!message) {
			message = "";
		}
		if (!tip) {
			tip = "";
		}
		if (!buttons || buttons.length < 2) {
			buttons = ["确定", "取消"];
		}

		var html = "<div class='pop-wrap' id='message_prompt_pop'><div class='passwrod-bank-inp' style='min-width: 17em;'><h2>{0}</h2><pre class='tishiyu'>{1}</pre><p class='srxmk'><input type='text' id='message_prompt_input' class='qsrndxm' placeholder='{2}'></p><div class='pass-btn-block'><p class='queding' id='btnprompt0'>{3}</p><p class='quxiao' id='btnprompt1'>{4}</p></div></div></div>";
		html = html.format(title, message, tip, buttons[0], buttons[1]);
		$(html).appendTo("body");

		$("#message_prompt_input").focus();

		var pop = $("#message_prompt_pop");
		$("#btnprompt0").on("tap", function() {
			var value = $("#message_prompt_input").val();
			pop.remove();
			if (promptCB) promptCB(0, value);
		});
		$("#btnprompt1").on("tap", function() {
			var value = $("#message_prompt_input").val();
			pop.remove();
			if (promptCB) promptCB(1, value);
		});
	};

	$.toast = function(message) {
		if ($("div[name=message_toast]").length) {
			return;
		}
		// 默认值
		if (!message) {
			message = "";
		}
		var options = {
			duration: 1000,
			top: "",
			left: ""
		};

		var html = "<div name='message_toast' class='pop-wrap' style=''><i class='toastmsg'>{0}</i></div>";
		html = html.format(message);
		$(html).appendTo("body");

		var msg = $("div[name=message_toast]");
		msg.fadeIn(1000);
		setTimeout(function() {
			msg.fadeOut(1200);
			setTimeout(function() {
				msg.remove();
			}, 1500);
		}, options.duration);
	};

	// 获取IP
	$.getIPAddress = function(refresh) {
		var ip = getLocalStorage(STORAGE_MANAGER.ip);
		if (!ip || refresh) {
			$.getScript("http://pv.sohu.com/cityjson?ie=utf-8").done(function() {
				setLocalStorage(STORAGE_MANAGER.ip, returnCitySN.cip);
				return returnCitySN.cip;
			}).fail(function(res) {
				setLocalStorage(STORAGE_MANAGER.ip, "127.0.0.1");
				return "127.0.0.1";
			});
		} else {
			return ip;
		}
	}

	$.focus = function(element) {
		if (os.ios) {
			setTimeout(function() {
				element.focus();
			}, 10);
		} else {
			element.focus();
		}
	};	
});


//实现字符串trime
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

//实现字符串format功能
String.prototype.format = function(args) {
	var result = this;
	if (arguments.length > 0) {
		if (arguments.length == 1 && typeof(args) == "object") {
			for (var key in args) {
				if (args[key] != undefined) {
					var reg = new RegExp("({" + key + "})", "g");
					result = result.replace(reg, args[key]);
				}
			}
		} else {
			for (var i = 0; i < arguments.length; i++) {
				if (arguments[i] != undefined) {
					var reg = new RegExp("({[" + i + "]})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
	}
	return result;
}

//实现Date的format功能例如：(new Date()).Format('yyyy-MM-dd hh:mm:ss');
Date.prototype.Format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

//timespan转换成时间字符串例如：toDateString(1442963412, 'yyyy-MM-dd hh:mm:ss')
function toDateString(timespan, fmt) {
	var newDate = new Date();
	newDate.setTime(timespan * 1000);
	return newDate.Format(fmt);
}

// 设置localstorage
function setLocalStorage(key, value) {
	localStorage.setItem(key, JSON.stringify(value));
}

// 读取localstorage
function getLocalStorage(key) {
	var obj = localStorage.getItem(key);
	if (obj) {
		return JSON.parse(obj);
	}
	return null;
}

//删除localstorage
function removeLocalStorage(key) {
	localStorage.removeItem(key);
}

// 设置sessionstorage
function setSessionStorage(key, value) {
	sessionStorage.setItem(key, JSON.stringify(value));
}

// 读取sessionstorage
function getSessionStorage(key) {
	var obj = sessionStorage.getItem(key);
	if (obj) {
		return JSON.parse(obj);
	}
	return null;
}

//删除sessionstorage
function removeSessionStorage(key) {
	sessionStorage.removeItem(key);
}

// localStorage
function setPageParams(pagename, value) {
	localStorage.setItem(pagename, JSON.stringify(value));
}

// 获取URL中参数
function getUrlParam(name, url) {
    var paramstr = url ? url.substr(url.lastIndexOf('?')) : window.location.search;
    paramstr = decodeURI(paramstr);
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = paramstr.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}











(function(w) {
	// 处理返回事件
	w.back = function(hide) {
		if (history.length > 1) {
			history.back();
		} else {
			w.close();
		}
	};
	// 处理点击事件
	var openw = null,
		waiting = null;
	/**
	 * 打开新窗口
	 * @param {URIString} id : 要打开页面url
	 */
	w.redirect = function(url) {
		var index = window.location.href.lastIndexOf('/');
		var url = window.location.href.substring(0,(index+1)) + url;
		window.location.href = encodeURI(url);
	};

	/**
	 * 关闭等待框
	 */
	w.closeWaiting = function() {
		waiting && waiting.close();
		waiting = null;
	}
})(window);